package com.github.binarylei.wordcount.bolt;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.IRichBolt;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.tuple.Tuple;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: leigang
 * @version: 2018-04-07
 */
public class WorldReportBolt implements IRichBolt {

    private static final long serialVersionUID = 1L;
    private Map<String, Long> counts;

    @Override
    public void prepare(Map map, TopologyContext topologyContext, OutputCollector collector) {
        counts = new HashMap<>();
    }

    @Override
    public void execute(Tuple tuple) {
        String word = tuple.getStringByField("word");
        Long count = tuple.getLongByField("count");
        counts.put(word, count);
    }

    @Override
    public void cleanup() {
        System.out.println("===================================");
        for (Map.Entry<String, Long> me : counts.entrySet()) {
            System.out.println(me.getKey() + "：" + me.getValue());
        }
        System.out.println("===================================");
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {

    }

    @Override
    public Map<String, Object> getComponentConfiguration() {
        return null;
    }
}
